#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n)
    {
        ListNode* fast = head;
        ListNode* prevslow = nullptr;
        ListNode* slow = head;

        while (n--)
        {
            fast = fast->next;
        }

        while (fast)
        {
            fast = fast->next;
            prevslow = slow;
            slow = slow->next;
        }
        if (prevslow == nullptr)
            return head->next;

        prevslow->next = slow->next;
        return head;
    }
};